/*
 Copyright (c) 2014, Carlos Eduardo Klock.
 All rights reserved.

 Redistribution and use in source and binary forms, with or without modification, are permitted provided that the following conditions are met:

 1. Redistributions of source code must retain the above copyright notice, this list of conditions and the following disclaimer.

 2. Redistributions in binary form must reproduce the above copyright notice, this list of conditions and the following disclaimer in the documentation and/or other
 materials provided with the distribution.

 3. Neither the name of the copyright holder nor the names of its contributors may be used to endorse or promote products derived from this software without specific
 prior written permission.

 THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
 OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
 INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS;
 OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
 ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
 */

package com.googlecode.fxutils;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collection;
import java.util.List;
import javafx.geometry.Orientation;
import javafx.geometry.Pos;
import javafx.scene.Parent;
import javafx.scene.Scene;
import javafx.scene.control.Button;
import javafx.scene.control.Control;
import javafx.scene.control.Label;
import javafx.scene.layout.HBox;
import javafx.scene.layout.Pane;
import javafx.scene.layout.VBox;
import javafx.stage.Stage;
import javafx.stage.StageStyle;
import javafx.stage.Window;

/**
 *
 * @author Carlos Eduardo Klock
 */
public class FxUtils {

    public enum SameSize{WIDTH, HEIGHT, WIDTH_HEIGHT, NONE};

    private FxUtils() {
    }

    /**
     * Displays and hides the node, so the sizes of its children are known.
     * @param p  the parent node.
     */
    public static void build(final Parent p) {
        Stage s = new Stage(StageStyle.UNDECORATED);
        Scene scene = new Scene(p);
        s.setScene(scene);
        s.setX(-1000000);
        s.setY(-1000000);
        s.show();
        s.hide();
        s.setScene(null);
        scene.setRoot(new Label());
    }

    /**
     * Centralizes a window on the owner.
     * @param w       the window.
     * @param owner   the owner window.
     */
    public static void centralizeOnOwner(final Window w, final Window owner) {
        w.setX(owner.getX() + owner.getWidth()/2 - w.getWidth()/2);
        w.setY(owner.getY() + owner.getHeight()/2 - w.getHeight()/2);
    }

    public static double minWidth(final Collection<? extends Control> nodes) {
        double min = Double.MAX_VALUE;
        for (Control c : nodes) {
            double value = c.getWidth();
            if (value < min) {
                min = value;
            }
        }
        return min;
    }

    public static double maxWidth(final Collection<? extends Control> nodes) {
        double max = -1;
        for (Control c : nodes) {
            double value = c.getWidth();
            if (value > max) {
                max = value;
            }
        }
        return max;
    }

    public static double minHeight(final Collection<? extends Control> nodes) {
        double min = Double.MAX_VALUE;
        for (Control c : nodes) {
            double value = c.getHeight();
            if (value < min) {
                min = value;
            }
        }
        return min;
    }

    public static double maxHeight(final Collection<? extends Control> nodes) {
        double max = -1;
        for (Control c : nodes) {
            double value = c.getHeight();
            if (value > max) {
                max = value;
            }
        }
        return max;
    }

    public static List<Button> createButtons(final String... buttons) {
        return createButtons(Arrays.asList(buttons));
    }

    public static List<Button> createButtons(final Collection<String> buttons) {
        List<Button> list = new ArrayList<>(buttons.size());
        for (String b : buttons) {
            list.add(new Button(b));
        }
        return list;
    }

    public static Pane createButtonPane(final Orientation orientation, final Collection<Button> buttons, final Pos alignment, final double spacing, final SameSize sameSize) {
        final Pane pane;
        if (orientation == Orientation.HORIZONTAL) {
            HBox box = new HBox(spacing);
            box.setAlignment(alignment);
            box.getChildren().addAll(buttons);
            pane = box;
        } else if (orientation == Orientation.VERTICAL) {
            VBox box = new VBox(spacing);
            box.setAlignment(alignment);
            box.getChildren().addAll(buttons);
            pane = box;
        } else {
            throw new IllegalArgumentException();
        }

        build(pane);

        final double maxWidth = maxWidth(buttons);
        final double maxHeight = maxHeight(buttons);

        switch (sameSize) {
            case WIDTH:
                for (Button b : buttons) {
                    b.setPrefWidth(maxWidth);
                }
                break;
            case HEIGHT:
                for (Button b : buttons) {
                    b.setPrefHeight(maxHeight);
                }
                break;
            case WIDTH_HEIGHT:
                for (Button b : buttons) {
                    b.setPrefWidth(maxWidth);
                    b.setPrefHeight(maxHeight);
                }
                break;
        }

        return pane;
    }

}
